﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class OutStockDetail : PageBase
{
    private string action
    {
        get
        {
            if (ViewState["action"] == null) return "";
            else return ViewState["action"].ToString();
        }
        set { ViewState["action"] = value; }
    }
    private string _OutStockID
    {
        get
        {
            if (ViewState["OutStockID"] == null) return "";
            else return ViewState["OutStockID"].ToString();
        }
        set { ViewState["OutStockID"] = value; }

    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            ViewState["EditItem"] = false;
            Client.DataSource = new ClientBLL().GetClientList();
            Client.DataBind();
            CommomMethod.BindEnumForDropdwonList(this.Type, typeof(Enumerations.OutStockType));
            action = Request.QueryString["action"];
            if (action ==null||action==""||action.ToString()=="add" )
            {
                action = "add";
                lblTitle.Text = "添加出库单";
                CreateTime.Text = DateTime.Now.ToShortDateString();

                this.btnReport.Visible = false;
                this.btnReport2.Visible = false;
                
            }
            else if (action == "edit")
            {
                lblTitle.Text = "编辑出库单";
                _OutStockID=Request.QueryString["ID"];
                DetailDiv.Visible = true;
                MainDelete.Visible = true;
                MainFinish.Visible = true;
                this.MainSave.Visible = true;
                this.btnReport.Visible = false;
                this.btnReport2.Visible = false;

                OutStockBLL BllOutStock = new OutStockBLL();
                T_OUTSTOCK OutStock=BllOutStock.GetOutStockByID(_OutStockID);
                SearchResult.DataSource = OutStock.T_OUTSTOCK_ITEMs;
                SearchResult.DataBind();

                this.CreateTime.Text = OutStock.CREATE_TIME.Value.ToShortDateString();
                this.Trade.Text = OutStock.TRADE;
                this.Transfer.Text = OutStock.TRANSFER;
                this.TransferNO.Text = OutStock.TRANSFER_NO;
                this.CarNO.Text = OutStock.CAR_NO;
                this.RawID.Text = OutStock.RawID;
                this.ApplyID.Text = OutStock.APPLY_ID;
                this.OutStockDate.Text = OutStock.OUTSTOCK_DATE.Value.ToShortDateString();
                this.OutStockID.Text = OutStock.OUTSTOCK_ID;
                this.txtRemark.Text = OutStock.Remark;
                ListItem Item = Client.Items.FindByValue(OutStock.CLIENT_ID.ToString());
                Client.SelectedIndex = Client.Items.IndexOf(Item);
                this.Type.SelectedIndex = OutStock.TYPE.Value;

            }
            else
            {
                lblTitle.Text = "查看出库单";
                _OutStockID = Request.QueryString["ID"];
                DetailDiv.Visible = false;
                MainDelete.Visible = false;
                MainFinish.Visible = false;
                MainSave.Visible = false;
                this.SearchResult.Columns[7].Visible = false;

                OutStockBLL BllOutStock = new OutStockBLL();
                T_OUTSTOCK OutStock = BllOutStock.GetOutStockByID(_OutStockID);
                SearchResult.DataSource = OutStock.T_OUTSTOCK_ITEMs;
                SearchResult.DataBind();

                this.CreateTime.Text = OutStock.CREATE_TIME.Value.ToShortDateString();
                this.Trade.Text = OutStock.TRADE;
                this.Transfer.Text = OutStock.TRANSFER;
                this.TransferNO.Text = OutStock.TRANSFER_NO;
                this.CarNO.Text = OutStock.CAR_NO;
                this.OutStockDate.Text = OutStock.OUTSTOCK_DATE.Value.ToShortDateString();
                this.OutStockID.Text = OutStock.OUTSTOCK_ID;
                this.RawID.Text = OutStock.RawID;
                this.ApplyID.Text=  OutStock.APPLY_ID;
                this.txtRemark.Text = OutStock.Remark;
                ListItem Item = Client.Items.FindByValue(OutStock.CLIENT_ID.ToString());
                Client.SelectedIndex = Client.Items.IndexOf(Item);
                this.Type.SelectedIndex = OutStock.TYPE.Value;
                
            }

        }
        CreateTime.ReadOnly = true;
        OutStockDate.Attributes["readonly"] = "readonly";
        OutStockID.Attributes["readonly"] = "readonly";
     }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        T_STORE Store = new StoreBLL().GetStoreByID(StoreID.Text.Trim());
        if (Store != null)
        {
            ProductsName.Value = Store.T_PRODUCT.NAME;
            Model.Value = Store.T_PRODUCT_DETAIL.MODEL;
            Standard.Value = Store.T_PRODUCT_DETAIL.STANDARD;
            Origin.Value = Store.T_PRODUCT_DETAIL.ORIGIN;
            Brand.Value = Store.T_PRODUCT_DETAIL.BRAND;
           // PalletQty.Value = Convert.ToString(Store.T_PRODUCT_DETAIL.PALLET_QTY);
            Size.Value = Store.T_PRODUCT_DETAIL.SIZE;
            LotNumber.Value = Store.T_PRODUCT_DETAIL.LOT_NUMBER;
            SubLotNumber.Value = Store.T_PRODUCT_DETAIL.SUB_LOT_NUMBER;
            PacketNO.Value = Store.T_PRODUCT_DETAIL.PACKET_NO;
            if(Store.T_PRODUCT_DETAIL.PRODUCTED_DATE == null) ProductedDate.Value = ""; else ProductedDate.Value = Convert.ToString(Store.T_PRODUCT_DETAIL.PRODUCTED_DATE.Value.ToShortDateString());            
            Stock.Value = Store.T_FLOOR.T_STOCK_AREA.T_STOCK.STOCK_NAME;
            Area.Value = Store.T_FLOOR.T_STOCK_AREA.AREA_NAME;
            Floor.Value = Store.T_FLOOR.FloorName;
            FloorID.Value = Convert.ToString(Store.FloorID);
            BarCode.Value = Store.T_PRODUCT_DETAIL.BAR_CODE;
            ProductDetailID.Value = Store.PRODUCT_DETAIL_ID.ToString();
            this.StockType.Value=Enum.GetName(typeof(Enumerations.StockType),Store.T_PRODUCT_DETAIL.STOCK_TYPE);
            this.KeyStandard.Value = Store.T_PRODUCT_DETAIL.KeyStandard;
            this.SupplyLotNumber.Value = Store.T_PRODUCT_DETAIL.SupplyLotNumber;
            this.BigClass.Value = Store.T_PRODUCT_DETAIL.BigClass;
            if (Store.T_PRODUCT.T_UNIT1 != null)
            {
                this.NewWeightUnit.Text = this.GrossWeightUnit.Text = Store.T_PRODUCT.T_UNIT1.NAME;
            }
            if (Store.T_PRODUCT.T_UNIT != null)   
            {
                this.PacketQtyUnit.Text = this.PacketQty2Unit.Text = Store.T_PRODUCT.T_UNIT.NAME;
            }
            // OtherStoreDate.Value = Convert.ToString(Store.T_PRODUCT_DETAIL.OTHER_STOCK_DATE.Value.ToShortDateString());

        }
    }
    protected string GetOutStockID()
    {
        string outStockID = "1" + DateTime.Now.ToString("yyyyMMdd");
        OutStockBLL outStockBLL = new OutStockBLL();

        T_OUTSTOCK outStock = outStockBLL.GetOutStockList().Where(t => t.OUTSTOCK_ID.Substring(1, 8) == DateTime.Now.ToString("yyyyMMdd")).OrderByDescending(y => y.OUTSTOCK_ID).FirstOrDefault();
        if (outStock != null)
        {
            int no = Int32.Parse(outStock.OUTSTOCK_ID.Substring(9).TrimStart(new char[] { '0' })) + 1;
            outStockID += no.ToString().PadLeft(3, '0');
        }
        else
        {
            outStockID += "001";
        }

        return outStockID;
    }
    protected void MainSave_Click(object sender, EventArgs e)
    {
        OutStockBLL BllOutStock = new OutStockBLL();

        if (action == "add" &&string.IsNullOrEmpty( _OutStockID))
        {
            OutStockID.Text = GetOutStockID();
            T_OUTSTOCK OutStock = new T_OUTSTOCK();
            string NewOutStockID = string.Empty;
            OutStock.CAR_NO = CarNO.Text.Trim();
            OutStock.CLIENT_ID = new Guid(Client.SelectedValue);
            OutStock.CREATE_TIME = DateTime.Now;
            OutStock.CREATOR_ID = this.UserInfo.ID;
            DateTime result;
            if (!DateTime.TryParse(OutStockDate.Text.Trim(),out result))
            {
                ShowMessage("出仓日期不合法");
                return;
            }
            OutStock.OUTSTOCK_DATE = result;
            OutStock.OUTSTOCK_ID = OutStockID.Text.Trim();
            OutStock.STATUS = (int)Enumerations.StockStatus.提交审核中;
            OutStock.TRADE = Trade.Text.Trim();
            OutStock.TRANSFER = Transfer.Text.Trim();
            OutStock.TRANSFER_NO = TransferNO.Text.Trim();
            OutStock.TYPE = Convert.ToInt32(Type.SelectedValue);
            OutStock.RawID = this.RawID.Text.Trim();
            OutStock.APPLY_ID = this.ApplyID.Text.Trim();
            OutStock.Remark = this.txtRemark.Text.Trim();
            BllOutStock.AddOutStock(OutStock, out NewOutStockID);
            _OutStockID = NewOutStockID;
            DetailDiv.Visible = true;
            MainDelete.Visible = true;
            MainFinish.Visible = true;
            btnReport.Visible = true;
            btnReport2.Visible = true;
        }
        else
        {
            T_OUTSTOCK OutStock = BllOutStock.GetOutStockByID(_OutStockID);

            OutStock.CAR_NO = CarNO.Text.Trim();
            OutStock.CLIENT_ID = new Guid(Client.SelectedValue);
            //更新的时候不要更新createtime
            //OutStock.CREATE_TIME = DateTime.Now;
            OutStock.CREATOR_ID = this.UserInfo.ID;
            DateTime result;
            if (!DateTime.TryParse(OutStockDate.Text.Trim(), out result))
            {
                ShowMessage("出仓日期不合法");
                return;
            }
            OutStock.OUTSTOCK_DATE = result;
            OutStock.OUTSTOCK_ID = OutStockID.Text.Trim();
            OutStock.STATUS = (int)Enumerations.StockStatus.提交审核中;
            OutStock.TRADE = Trade.Text.Trim();
            OutStock.TRANSFER = Transfer.Text.Trim();
            OutStock.TRANSFER_NO = TransferNO.Text.Trim();
            OutStock.TYPE = Convert.ToInt32(Type.SelectedValue);
            OutStock.RawID = this.RawID.Text.Trim();
            OutStock.APPLY_ID = this.ApplyID.Text.Trim();
            OutStock.Remark = this.txtRemark.Text.Trim();
            BllOutStock.UpdateOutStock(OutStock);
        }

    }
    protected void MainDelete_Click(object sender, EventArgs e)
    {
        new OutStockBLL().DeleteOutStock(_OutStockID);
        //CreateTime.Text = string.Empty;
        OutStockID.Text = string.Empty;
        Trade.Text = string.Empty;
        Transfer.Text = string.Empty;
        CarNO.Text = string.Empty;
        TransferNO.Text = string.Empty;
        OutStockDate.Text = string.Empty;
        RawID.Text = string.Empty;
        DetailDiv.Visible = false;
        MainDelete.Visible = false;
        MainFinish.Visible = false;
        SearchResult.DataSource = null;
        SearchResult.DataBind();
        _OutStockID = "";
        action = "add";
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        OutStockItemBLL BllOutStockItem = new OutStockItemBLL();
        ProductDetailBLL BllProductDetail = new ProductDetailBLL();
        T_OUTSTOCK_ITEM Item;
        T_PRODUCT_DETAIL detail = BllProductDetail.GetProductDetailByID(ProductDetailID.Value);
        if ((bool)ViewState["EditItem"] == false)
        {
            Item = new T_OUTSTOCK_ITEM();
            Item.OUTSTOCK_ID = new Guid(_OutStockID);
        }
        else
        {
            Item = BllOutStockItem.GetOutStockItemByID(SearchResult.SelectedValue.ToString());
           
        }


        if (FloorID.Value.Trim() != string.Empty)
            Item.FLOOR_ID = new Guid(FloorID.Value);
        if (GrossWeight.Value.Trim() != string.Empty)
        {
            double pq;
            if (!double.TryParse(GrossWeight.Value.Trim(), out pq))
            {
                ShowMessage("毛重必须为数值类型");
                return;
            }
            Item.GROSS_WEIGHT = pq;
        }
        else
        {
            ShowMessage("毛重不能为空");
            return;
        }
        if (NetWeight.Value.Trim() != string.Empty)
        {
            double pq;
            if (!double.TryParse(NetWeight.Value.Trim(), out pq))
            {
                ShowMessage("净重必须为数值类型");
                return;
            }
            Item.NET_WEIGHT = pq;
        }
        else
        {
            ShowMessage("净重不能为空");
            return;
        }
       
        if (PacketQty.Value.Trim() != string.Empty)
        {
            double pq;
            if(!double.TryParse(PacketQty.Value.Trim(),out pq))
            {
                ShowMessage("大件数必须为数值类型");
                return;
            }
            Item.PACKET_QTY = pq;
        }
        else
        {
            ShowMessage("大件数不能为空");
            return;
        }
        if (PacketQty2.Value.Trim() != string.Empty)
        {
            double pq;
            if (!double.TryParse(PacketQty2.Value.Trim(), out pq))
            {
                ShowMessage("小件数必须为数值类型");
                return;
            }
            Item.PACKET_QTY2 = pq;
        }
        else
        {
            ShowMessage("小件数不能为空");
            return;
        }
        Item.REMARK = Remark.Value;
        Item.PRODUCT_DETAIL_ID = new Guid(ProductDetailID.Value);

        if ((bool)ViewState["EditItem"] == false)
        {
            BllOutStockItem.AddOutStockItem(Item);
        }
        else {
            BllOutStockItem.UpdateOutStockItem(Item);
            ProductDetailID.Value = string.Empty;
            ProductsName.Value = string.Empty;
            this.BarCode.Value = string.Empty;
            this.Model.Value = string.Empty;
            this.Standard.Value = string.Empty;
            this.KeyStandard.Value = string.Empty;
            this.Origin.Value = string.Empty;
            this.Brand.Value = string.Empty;
            this.Size.Value = string.Empty;
            this.LotNumber.Value = string.Empty;
            this.SubLotNumber.Value = string.Empty;
            this.SupplyLotNumber.Value = string.Empty;
            this.BigClass.Value = string.Empty;
            this.ProductsName.Value = string.Empty;
            this.PacketNO.Value = string.Empty;

            this.NetWeight.Value = string.Empty;
            this.GrossWeight.Value = string.Empty;
            this.PacketQty.Value = string.Empty;
            this.PacketQty2.Value = string.Empty;
            this.Stock.Value = string.Empty;
            this.Area.Value = string.Empty;
            this.Floor.Value = string.Empty;
            this.FloorID.Value = string.Empty;
            this.Remark.Value = string.Empty;
        }
       
       
        //detail.STANDARD = Standard.Value;
        //BllProductDetail.UpdateProductDetail(detail);
        BindGridView();
        SearchResult.SelectedIndex = -1;


        ViewState["EditItem"] = false;
       
        ShowMessage("保存成功");
        
    }

    protected void BindGridView()
    {
        OutStockBLL BllOutStock = new OutStockBLL();
        BllOutStock.AddOrderByCondition(ViewState["StationSortOrder"] + " " + ViewState["StationOrderDire"]);
        SearchResult.DataSource = BllOutStock.GetOutStockDetailByStockID(_OutStockID);
        SearchResult.DataBind();
    }

    protected void MainFinish_Click(object sender, EventArgs e)
    {
        if (action == null || action == "" || action.ToString() == "add")
        {
            Response.Redirect("OutStockDetail.aspx?action=add");
        }
        else
        {
            Response.Redirect("OutStockManager.aspx");
        }
    }
    protected void SearchResult_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
            string ID = SearchResult.DataKeys[e.RowIndex]["ID"].ToString();
            OutStockItemBLL BllOutStockItem = new OutStockItemBLL();
            BllOutStockItem.DeleteOutStockItem(ID);

            BindGridView();
    }
    //protected void MainFinish0_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("OutStockManager.aspx");
    //}
    protected void SearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchResult.PageIndex = e.NewPageIndex;
        BindGridView();
    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.ID)) return;
        //Response.Redirect("~/report/ReportView.aspx?type=outstockdetail&ID=" + _OutStockID);    
        string js = "<script language='javascript'>window.open(\"../report/ReportView.aspx?type=outstockdetail&ID=" + _OutStockID + "\",\"_blank\");</script>";
        Response.Write(js);
    }
    protected void btnReport2_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.ID)) return;
        // Response.Redirect("~/report/ReportView.aspx?type=instock&ID="+ID);    
        string js = "<script language='javascript'>window.open(\"../report/ReportView.aspx?type=outstocknofloor&ID=" + _OutStockID + "\",\"_blank\");</script>";
        Response.Write(js);
    }
    protected void SearchResult_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        if (Convert.ToString(ViewState["StationSortOrder"]) == sortExpression)
        {
            if (Convert.ToString(ViewState["StationOrderDire"])== "Desc")
                ViewState["StationOrderDire"] = "Asc";
            else
                ViewState["StationOrderDire"] = "Desc";
        }
        else
        {
            ViewState["StationSortOrder"] = sortExpression;
        }
        BindGridView();
    }
    protected string GetStockPosition(T_OUTSTOCK_ITEM item)
    {
        return item.T_FLOOR.T_STOCK_AREA.T_STOCK.STOCK_NAME + "->" + item.T_FLOOR.T_STOCK_AREA.AREA_NAME + "->" + item.T_FLOOR.FloorName;
    
    }
    protected void SearchResult_SelectedIndexChanged(object sender, EventArgs e)
    {
        T_OUTSTOCK_ITEM item = new OutStockItemBLL().GetOutStockItemByID(SearchResult.SelectedValue.ToString());
        this.ProductDetailID.Value = item.T_PRODUCT_DETAIL.ID.ToString();
        this.BarCode.Value = item.T_PRODUCT_DETAIL.BAR_CODE;
        this.Model.Value = item.T_PRODUCT_DETAIL.MODEL;
        this.Standard.Value = item.T_PRODUCT_DETAIL.STANDARD;
        this.KeyStandard.Value = item.T_PRODUCT_DETAIL.KeyStandard;
        this.Origin.Value = item.T_PRODUCT_DETAIL.ORIGIN;
        this.Brand.Value = item.T_PRODUCT_DETAIL.BRAND;
        this.Size.Value = item.T_PRODUCT_DETAIL.SIZE;
        this.LotNumber.Value = item.T_PRODUCT_DETAIL.LOT_NUMBER;
        this.SubLotNumber.Value = item.T_PRODUCT_DETAIL.SUB_LOT_NUMBER;
        this.SupplyLotNumber.Value = item.T_PRODUCT_DETAIL.SupplyLotNumber;
        this.BigClass.Value = item.T_PRODUCT_DETAIL.BigClass;
        this.ProductsName.Value = item.T_PRODUCT_DETAIL.T_PRODUCT.NAME;
        if (item.T_PRODUCT_DETAIL.PRODUCTED_DATE != null)
            this.ProductedDate.Value = item.T_PRODUCT_DETAIL.PRODUCTED_DATE.Value.ToString();

        this.PacketNO.Value = item.T_PRODUCT_DETAIL.PACKET_NO;

        var areaId = new FloorBLL().GetFloorByID(item.FLOOR_ID.ToString()).AREA_ID;
        var stockId = new StoreHouseBLL().GetStockAreaByID(areaId.Value.ToString()).STOCK_ID;

     
        if (item.T_PRODUCT_DETAIL.STOCK_TYPE != null)
            this.StockType.Value = item.T_PRODUCT_DETAIL.STOCK_TYPE.Value.ToString();

        this.PacketNO.Value = item.T_PRODUCT_DETAIL.PACKET_NO;

        this.NetWeight.Value = item.NET_WEIGHT.ToString();
        this.GrossWeight.Value = item.GROSS_WEIGHT.ToString();
        this.PacketQty.Value = item.PACKET_QTY.ToString();
        this.PacketQty2.Value = item.PACKET_QTY2.ToString();
        this.Stock.Value = item.T_FLOOR.T_STOCK_AREA.T_STOCK.STOCK_NAME;
        this.Area.Value = item.T_FLOOR.T_STOCK_AREA.AREA_NAME;
        this.Floor.Value = item.T_FLOOR.FloorName;
        this.FloorID.Value = item.T_FLOOR.FloorID.ToString();
        this.Remark.Value = item.REMARK;

        ViewState["ProductDetailID"] = item.T_PRODUCT_DETAIL.ID.ToString();
        ViewState["EditItem"] = true;
    }
}
